%    Copyright (C) 2000 artofcode LLC.  All rights reserved.
% 
% This software is provided AS-IS with no warranty, either express or
% implied.
% 
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
% 
% For more information about licensing, please refer to
% http://www.ghostscript.com/licensing/. For information on
% commercial licensing, go to http://www.artifex.com/licensing/ or
% contact Artifex Software, Inc., 101 Lucas Valley Road #110,
% San Rafael, CA  94903, U.S.A., +1(415)492-9861.

% $Id: gs_resst.ps 6300 2005-12-28 19:56:24Z giles $
% Redefine generic category with static resource table.

languagelevel 2 .setlanguagelevel

15 dict begin    % a temporary dictionary for local binding.

/sep (/) def %  The %static%  device uses platform independent paths.

/tempstring 8192 string def

/IsMyRecord      % <raw_record> IsMyRecord <raw_record> bool
{ dup type /dicttype eq { dup /StaticFilePos known } { false } ifelse
} bind def

/append_string    % <scratch> <index> <string> append_string <scratch> <index> 
{
   dup length 3 index 3 index 2 index getinterval   % (scratch) index (string) l (scr_substr)
   3 2 roll exch copy pop add                       % (scratch) index'
} bind def

/StaticResourceRecordVirtualMethods 4 dict begin

  /GetFilePath    % <scratch> <Name> <record> GetFilePath <filepath>
  { pop exch                                                % /Name (scratch)
    (%static%) dup length exch 2 index copy pop             % /Name (scratch) l
    //sep //append_string exec                              % /Name (scratch) l
    Category //tempstring cvs //append_string exec          % /Name (scratch) l
    //sep //append_string exec                              % /Name (scratch) l
    3 2 roll //tempstring cvs                               % (scratch) l (Name)
    //append_string exec                                    % (scratch) l
    0 exch getinterval                                      % (path)
  } bind def

  /MakeInstance   % <Name> <record> MakeInstance <Name> <Instance> <size>
  {
    begin
      STRESDEBUG { (Loading a static resource ) print dup = } if
      8192 string 2 index 2 index //GetFilePath exec
      (r) file run
      STRESDEBUG { (Completed loading the static resource ) print dup = } if
      StaticFileEnd StaticFilePos sub
    end
    1 index Category findresource
    exch
  } bind def

  /GetSize        % <Name> <record> GetSize <size>
  {
    % WARNING : this retrives file sise rather than VM size
    begin
    pop
    StaticFileEnd StaticFilePos sub
    end
  } bind def

  /IsActive       % <record> IsActive <bool>
  { pop true
  } bind def

currentdict end def

mark
/MappedCategoryRedefiner /ProcSet findresource begin

/.StaticResourcesBeg % <end_string> <no_of_resources> <category_name> .StaticResourcesBeg <category_name>
%     Puts .map dictionary to dstack.
{
  STRESDEBUG { (Starting static resources for category ) print dup = } if
  dup /Category resourcestatus {
    pop pop exch dict begin exch pop
  } {
    STRESDEBUG { (Skipping the resources because the category isn't defined.) = } if
    pop pop .skipeof
  } ifelse
} bind

/.StaticResourcesEnd % <category_name> .StaticResourcesEnd -
%     Removes .map dictionary from dstack.
{
  STRESDEBUG { (Completting static resources for category ) print dup = } if
  currentdict end dup                                      % /CatName <map> <map>
  systemdict /.io_static_root .knownget not {
    10 dict                                                % /CatName <map> <map> <dir>
    % Save it for garbager and for debug purpose :
    dup systemdict exch /.io_static_root exch put          % /CatName <map> <map> <dir>
    dup .setup_io_static                                   % /CatName <map> <map> <dir>
  } if                                                     % /CatName <map> <map> <dir>
  exch 3 index exch put                                    % /CatName <map>
  14 dict begin
    /.map exch def                                         % /CatName
    /CategoryName exch def                                 %
    /IsMapFileOptional true def
    /VerifyMap  { pop } bind def
    /PreprocessRecord  % <map> <Name> <raw_record> PreprocessRecord <map> <Name> <record> <bool>
    { //IsMyRecord exec dup {
        pop dup /RecordVirtualMethods //StaticResourceRecordVirtualMethods put
        true
      } if
    } bind def
  currentdict end
  //Redefine exec
} bind

end  % MappedCategoryRedefiner
end  % the temporary dictionary for local binding.

/.BeginResource  % <instance_name> BeginResource <instance_name> 
%     Puts a record dictionary to dstack.
{
  STRESDEBUG { (Static resource ) print dup =string cvs print } if
  currentfile fileposition
  currentfile =string readline pop dup length 0 exch 10 .min getinterval (%% Replace) ne {
    % We are in COMPILE_INITS=1 .
    4 dict begin
    /StaticFilePos exch def    
  } {
    % We are in COMPILE_INITS=0 .
    pop
  } ifelse
  (%END_RESOURCE) .skipeof
} bind

/.EndResource  % <instance_name> EndResource -
%     Removes a record dictionary from dstack.
{
  currentdict /StaticFilePos known {
    /StaticFileEnd currentfile fileposition
    (.EndResource) length sub
    def
    STRESDEBUG { ( from position ) print StaticFilePos =string cvs print ( to position ) print StaticFileEnd = } if
    currentdict end def
  } {
    pop
    STRESDEBUG { ( ignored with COMPILE_INITS=0. ) = } if
  } ifelse
} bind

.dicttomark /StaticCategoryRedefiner exch /ProcSet defineresource pop

.setlanguagelevel
